package com.xuanwei.schedule.handler.coupon;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuanwei.dao.entity.XwCoupon;
import com.xuanwei.dao.mapper.XwCouponMapper;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/8/23 10:13
 **/
@JobHandler(value = "CouponExpire")
@Component
@Slf4j
public class CouponExpire extends IJobHandler {

    private XwCouponMapper couponMapper;

    public CouponExpire(XwCouponMapper couponMapper) {
        this.couponMapper = couponMapper;
    }

    @Override
    public ReturnT<String> execute(String s) throws Exception {
        updateStatus();
        return ReturnT.SUCCESS;
    }

    public void updateStatus() {
        XwCoupon coupon = new XwCoupon();
        coupon.setStatus(2);
        QueryWrapper<XwCoupon> wrapper = new QueryWrapper<>();
        wrapper.eq("`status`", 0);
        wrapper.eq("expire_type", 0);
        wrapper.lt("expire_end_time", LocalDate.now());
        couponMapper.update(coupon, wrapper);
    }
}
